package com.example.springbootdemo.demos.web.controller;

import com.example.springbootdemo.demos.web.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RequestMapping("/return")
//@RestController=// @ResponseBody @Controller一起写
@Controller//该注解用于返回视图
public class ReturnController {
    //返回静态视图
    @RequestMapping("/index")
    public String returnIndex(){
        return "/index.html";
    }
    //返回数据
    @ResponseBody//该注解用于返回视图所需要的数据
    @RequestMapping("/indexData")
    public String returnData(){
        return "返回视图所需要的数据";
    }
    ////返回HTML代码片段
    @ResponseBody
    @RequestMapping("/indexHTML")
    public String returnHTML(){
        return "<h1>返回HTML代码片段</h1>";
    }
    //返回json
    @ResponseBody
    @RequestMapping("/indexJson")
    public Person returnJson(){
        Person person = new Person();
        person.setId(1);
        person.setAge(12);
        person.setName("zhangsan");
        return person;
    }
    //设置状态码
    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(401);//通常表示没有登录
        return "设置状态码";
    }
    //
    //设置Header
    @ResponseBody
    @RequestMapping(value = "/setHeader",produces = "application/json;charset=utf8")
    public String setHeader(HttpServletResponse response){
        //设置Header
        response.setHeader("myhead","myhead");
        return "{'ok',1}";
    }
}
